<template>
  <div class="save">
    <div class="inner">
      <div class='message-container'>
        <input class="name" id="name" type="text" v-model="data.name" placeholder="昵称（不必填）">
        <div class="memos-content">
          <textarea v-model="data.message"></textarea>
          <div class="upload-container">
            <img v-if="data.url" :src="data.url">
            <span v-else>上传图片</span>
            <div id="upload-img"></div>
            <input @change="upload" ref="uploadInput" accept="image/*" type="file" class="upload">
          </div>
        </div>
        <p class="button-box">
          <button disabled id="address">{{ data.address }}</button>
          <button disabled id="time">{{ data.create_time }}</button>
          <button @click="publish" class='save-btn'>发表</button>
        </p>
      </div>
    </div>
  </div>
</template>

<script>
  import moment from 'moment'
  import axios from 'axios'

  import request from '~/assets/request'

  export default {
    data() {
      return {
        data: {
          name: '一个隐身的人',
          url: '',
          create_time: '当前时间',
          address: '定位中'
        }
      }
    },
    methods: {
      setTime() {
        this.data.create_time = moment().format('YYYY-MM-DD HH:mm:ss')
        this.timer = setTimeout(() => {
          this.setTime()
        }, 1000)
      },
      async getPos() {
        const {data: {data = {}}} = await axios.get('https://api.luzhongkuan.cn/api/location2')
        this.data.address = data.province + data.city
      },
      async upload() {
        const input = this.$refs.uploadInput
        const [file] = input.files
        if (!file) return
        if (URL) {
          this.data.url = URL.createObjectURL(file)
        }
        const fd = new FormData()
        fd.append('file', file)
        const {data: data} = await axios({
          url: 'https://api.luzhongkuan.cn/qn/upload',
          method: 'post',
          data: fd
        })
        this.data.url = data.data
      },
      async publish() {
        if (!this.data.message) {
          this.$toast('请输入内容')
          return
        }
        const res = await request({
          url: '/api/message',
          method: 'post',
          data: this.data
        }, {isCache: false})
        if (res.success) {
          this.$emit('refresh')
        }
        this.$toast('留言成功')
        this.data.message = ''
        this.data.url = ''
      }
    },
    mounted() {
      this.setTime()
      this.getPos()
    },
    beforeDestroy() {
      if (this.timer) clearTimeout(this.timer)
    }
  }
</script>

<style lang="scss" scoped>
  .memos-content {
    display: flex;
  }
  .memos-content textarea {
    width: 80%;
  }
  .memos-content>div {
    width: 20%;
    height: 150px;
    background: red;
    line-height: 150px;
    text-align: center;
    font-size: 22px;
    background: rgba(0, 0, 0, 0.05);
    position: relative;
  }
  .memos-content img {
    width: 100%;
    max-height: 100%;
  }
  .upload-container {
    min-width: 150px;
  }
  #upload-img {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
  }
  .message-item img {
    width: 100%;
    margin: 10px 0;
  }
  #address, #time {
    padding: 4px 10px;
    border-radius: 4px;
    background: transparent;
    border: 1px solid #e6e6e6;
  }
  .save {
    padding: 20px 0;
    margin: 0 auto;
    max-width: 1200px;
  }
  .save .inner {
    padding: 0 20px 20px;
    border: 1px solid #eee;
    box-sizing: border-box;
  }
  .save input {
    border: none;
    line-height: 40px;
    color: #666666;
  }

  .upload {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
  }
  .save textarea {
    width: 100%;
    height: 150px;
    border: 1px solid #d2d2d2;
    border-radius: 4px;
    padding: 20px;
    font-size: 16px;
    box-sizing: border-box;
  }
  .save .button-box {
    text-align: right;
    margin: 20px 0;
    button {
      margin-left: 10px;
    }
  }
  .save .save-btn {
    margin-left: 20px;
    height: 30px;
    background: #009688;
    border: none;
    padding: 0 18px;
    color: white;
  }
  @media (max-width: 768px) {
    .save textarea {
      height: 100px;
    }
    .memos-content > div {
      height: 100px;
      line-height: 100px;
      font-size: 18px;
    }
    .upload-container {
      min-width: 100px;
    }
  }
</style>
